﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TinyCinema.UI.Pages.AdminPanel
{
    /// <summary>
    /// Interaction logic for Backup.xaml
    /// </summary>
    public partial class Backup : Page
    {
        public static DependencyProperty FileNameProperty = DependencyProperty.Register("FileName", typeof(string), typeof(Backup));
        public string FileName
        {
            get { return (string)GetValue(FileNameProperty); }
            set
            {
                SetValue(FileNameProperty, value);
            }
        }
        public Backup()
        {
            InitializeComponent();
            Bussiness.AdminPanel.Backup.Messenger.Cleanup();
            Bussiness.AdminPanel.Backup.Messenger.Register<Bussiness.AdminPanel.Message.PermissionDenied>(this, (s) =>
            {
                MessageBoxUtils.WPFMessageBox.Show((App.Current.FindResource("langMsgPermissionDenied") as String), "", MessageBoxButton.OK);
            });
            Bussiness.AdminPanel.Backup.Messenger.Register<Bussiness.AdminPanel.Message.FileNotFound>(this, (s) =>
            {
                MessageBoxUtils.WPFMessageBox.Show((App.Current.FindResource("langMsgFileNotFound") as String), "", MessageBoxButton.OK);
            });
            Bussiness.AdminPanel.Backup.Messenger.Register<Bussiness.AdminPanel.Message.InputFileIsNotCorrect>(this, (s) =>
            {
                MessageBoxUtils.WPFMessageBox.Show((App.Current.FindResource("langMsgInputFileIsNotCorrect") as String), "", MessageBoxButton.OK);
            });
            Bussiness.AdminPanel.Backup.Messenger.Register<Bussiness.AdminPanel.Message.Restoring>(this, (s) =>
            {
                Cursor = Cursors.Wait;
            });
            Bussiness.AdminPanel.Backup.Messenger.Register<Bussiness.AdminPanel.Message.Restored>(this, (s) =>
            {
                Cursor = Cursors.Arrow;
                MessageBoxUtils.WPFMessageBox.Show((App.Current.FindResource("langMsgRestored") as String), "", MessageBoxButton.OK);
                MessageBoxUtils.WPFMessageBox.Show((App.Current.FindResource("langMsgRestartApp") as String), "", MessageBoxButton.OK);
                System.Diagnostics.Process.Start(AppDomain.CurrentDomain.BaseDirectory+"TinyCinema.exe");
                App.Current.Shutdown();         
            });
            Bussiness.AdminPanel.Backup.Messenger.Register<Bussiness.AdminPanel.Message.RestorFailed>(this, (s) =>
            {
                Cursor = Cursors.Arrow;
            });
            FileName = (App.Current.FindResource("langMsgSelectBackup") as String);
        }

        private void BackupDatabase(object sender, RoutedEventArgs e)
        {
            var dl = new Microsoft.Win32.SaveFileDialog();
            dl.FileName = "TinyCinema_" + DateTime.Now.Day + "_" + DateTime.Now.Month + "_" + DateTime.Now.Year;
            dl.DefaultExt = "bak";
            if (dl.ShowDialog().Value)
            {
                var filePath = dl.FileName;
                Bussiness.AdminPanel.Backup.BackupDatabase(filePath);
            }
        }
        private void RestoreDatabase(object sender, RoutedEventArgs e)
        {
            Bussiness.AdminPanel.Backup.RestoreDatabase(strFilePath);
        }
        string strFilePath;
        private void SelectFile(object sender, RoutedEventArgs e)
        {
            var dl = new Microsoft.Win32.OpenFileDialog();
            if(dl.ShowDialog().Value)
            {
                strFilePath = dl.FileName;
                FileName = System.IO.Path.GetFileName(strFilePath);
            }
        }
    }
}
